package com.ssyc.common.utils;

import android.text.InputFilter;
import android.text.Spanned;


/**
 * EditText限制输入的监听类
 * Created by Administrator on 2018/6/29 0029.
 */

public class MaxTextLengthFilter implements InputFilter {

    private int mMaxLength;

    public MaxTextLengthFilter(int max) {
        mMaxLength = max - 1;
    }

    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        int keep = mMaxLength - (dest.length() - (dend - dstart));
        if (keep < (end - start)) {
            UiUtils.Toast("超过" + (mMaxLength) + "字数限制", false);
        }
        if (keep <= 0) {
            return "";
        } else if (keep >= end - start) {
            return null;
        } else {
            return source.subSequence(start, start + keep);
        }
    }
}
